<?php

class NdkHealthNonController extends Controller
{
	/*
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	*/

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView()
	{
		$id = $_REQUEST['reg_nonhealth_policy_id'];

		$model=new NdkHealthNon('searchView');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['NdkHealthNon'])) {
			$model->attributes=$_GET['NdkHealthNon'];
		}

		$this->render('view',array(
			'model'=>$model,
			'id'=>$id,
		));
		
	}


	public function actionPosting()
	{	
		$id = $_REQUEST['reg_nonhealth_policy_id'];
		$model=new NdkHealthNon('searchView');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['NdkHealthNon'])) {
			$model->attributes=$_GET['NdkHealthNon'];
		}

		$this->render('posting',array(
			'model'=>$model,
			'id'=>$id,
		));
		
	}


	public function actionProsesPosting()
	{	
		$id = $_REQUEST['reg_nonhealth_policy_id'];
		$model=new NdkHealthNon('searchView');
		
		$sql = "update ndk_health_non set posting=2 where reg_nonhealth_policy_id=".$id;
		Yii::app()->db->createCommand($sql)->queryAll();

		$sql2 = "update ndk_health_non_master set posting=2 where reg_nonhealth_policy_id=".$id;
		Yii::app()->db->createCommand($sql2)->queryAll();

		$this->render('prosesPosting',array(
			'model'=>$model,
			'id'=>$id,
		));
		
	}

	public function actionCetak()
	{
		//if (isset($_POST['NdkHealth'])) {
			$reg_nonhealth_policy_id = $_REQUEST['reg_nonhealth_policy_id'];
			$master_id = $_REQUEST['master_id'];
			$paramsInput = array("reg_health_policy_id"=>(int)$reg_nonhealth_policy_id,"master_id"=>(int)$master_id);
			Yii::app()->printUtils->executeReport($paramsInput,'notadebetnonhealth/notadebet.jrxml','notadebet','notadebet');
		//}

		//$this->render('cetak');
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new NdkHealthNon;
		$model2=new MstClient('searchModal2');
		$model3=new RegHealthPolicyNon('search');

		$model2->unsetAttributes();  // clear any default values
		if (isset($_GET['MstClient'])) {
			$model2->attributes=$_GET['MstClient'];
		}
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		
		$temp_id = date("Ymdhis");

		if (isset($_POST['NdkHealthNon'])) {
			
			$modelNdkHealthMaster = new NdkHealthNonMaster;

			//--  Untuk Ndk Health Master --//
			$modelNdkHealthMaster->nota_debet_no = '0';
			$modelNdkHealthMaster->reg_nonhealth_policy_id = $_POST['NdkHealthNon']['reg_nonhealth_policy_id'];
			$modelNdkHealthMaster->endorsement_no = $_POST['NdkHealthNon']['endorsement_no'];
			$modelNdkHealthMaster->sum_insured = $_POST['NdkHealthNon']['sum_insured'];
			$modelNdkHealthMaster->rate = $_POST['NdkHealthNon']['rate'];
			$modelNdkHealthMaster->tpl = $_POST['NdkHealthNon']['tpl'];
			$modelNdkHealthMaster->total_premium = 0;
			$modelNdkHealthMaster->sisa_bayar = 0;
			$modelNdkHealthMaster->kurang_bayar = 0;
			$modelNdkHealthMaster->jml_bayar = 0;
			$modelNdkHealthMaster->is_bayar = '0';
			$modelNdkHealthMaster->client_tahu_diskon = $_POST['NdkHealthNon']['client_tahu_diskon'];
				$modelNdkHealthMaster->admin_broker = $_POST['NdkHealthNon']['admin_broker'];
				$modelNdkHealthMaster->discount = (int)$_POST['NdkHealthNon']['discount'];
				$modelNdkHealthMaster->discount_percent = $_POST['NdkHealthNon']['discount_percent'];
				$modelNdkHealthMaster->adjusment = $_POST['NdkHealthNon']['adjusment'];
				$modelNdkHealthMaster->due_date = $_POST['NdkHealthNon']['due_date'];
				$modelNdkHealthMaster->brokerage = $_POST['NdkHealthNon']['brokerage'];
				$modelNdkHealthMaster->brokerage_percent = $_POST['NdkHealthNon']['brokerage_percent'];
				$modelNdkHealthMaster->ppn = $_POST['NdkHealthNon']['ppn'];
				$modelNdkHealthMaster->ppn_percent =$_POST['NdkHealthNon']['ppn_percent'];
				$modelNdkHealthMaster->pph = $_POST['NdkHealthNon']['pph'];
				$modelNdkHealthMaster->pph_percent = $_POST['NdkHealthNon']['pph_percent'];
				$modelNdkHealthMaster->note = $_POST['NdkHealthNon']['note'];
				$modelNdkHealthMaster->createdby = Yii::app()->user->getId();
				$modelNdkHealthMaster->createdtime = date('Y-m-d',strtotime(trim($_POST['NdkHealthNon']['createdtime'])));
				$modelNdkHealthMaster->updatedby = Yii::app()->user->getId();
				$modelNdkHealthMaster->updatedtime = date('Y-m-d H:i:s');
				$modelNdkHealthMaster->installment_ke = 0;
				$modelNdkHealthMaster->stamp_duty = (int)$_POST['NdkHealthNon']['stamp_duty'] * count($due_date);
				$modelNdkHealthMaster->stampduty_asuransi = $_POST['NdkHealthNon']['stampduty_asuransi'];
				$modelNdkHealthMaster->policy_cost = $_POST['NdkHealthNon']['policy_cost'];
				$modelNdkHealthMaster->premium = (int)$_POST['NdkHealthNon']['premium'];
				$modelNdkHealthMaster->diskon_asuransi = (int)$_POST['NdkHealthNon']['diskon_asuransi'];
				$modelNdkHealthMaster->diskon_asuransi_percent = $_POST['NdkHealthNon']['diskon_asuransi_percent'];			
				$modelNdkHealthMaster->client_tahu_diskon = $_POST['NdkHealthNon']['client_tahu_diskon'];
				$modelNdkHealthMaster->posting = 1;
				$modelNdkHealthMaster->type = $_POST['NdkHealthNon']['type'];
				$modelNdkHealthMaster->value_brokerage = (int)$_POST['NdkHealthNon']['value_brokerage'];


				if((int)$_POST['NdkHealthNon']['client_tahu_diskon']==1){
				$modelNdkHealthMaster->nominal = (int)$_POST['NdkHealthNon']['premium']+(int)$_POST['NdkHealthNon']['adjusment']+(int)$_POST['NdkHealthNon']['tpl']+(int)$_POST['NdkHealthNon']['policy_cost']+(int)$_POST['NdkHealthNon']['stamp_duty']+(int)$_POST['NdkHealthNon']['stampduty_asuransi']+(int)$_POST['NdkHealthNon']['admin_broker']-(int)$_POST['NdkHealthNon']['discount'];	
				}
				else{
				$modelNdkHealthMaster->nominal = (int)$_POST['NdkHealthNon']['premium']+(int)$_POST['NdkHealthNon']['adjusment']+(int)$_POST['NdkHealthNon']['tpl']+(int)$_POST['NdkHealthNon']['policy_cost']+(int)$_POST['NdkHealthNon']['stamp_duty']+(int)$_POST['NdkHealthNon']['stampduty_asuransi']+(int)$_POST['NdkHealthNon']['admin_broker']-(int)$_POST['NdkHealthNon']['discount']-(int)$_POST['NdkHealthNon']['diskon_asuransi'];		
				}
				
				$modelNdkHealthMaster->nett_to_insurance = (int)trim($_POST['NdkHealthNon']['premium'])-(int)trim($_POST['NdkHealthNon']['diskon_asuransi'])+(int)trim($_POST['NdkHealthNon']['policy_cost'])+(int)trim($_POST['NdkHealthNon']['stampduty_asuransi'])-(int)trim($_POST['NdkHealthNon']['brokerage'])-(int)trim($_POST['NdkHealthNon']['ppn'])+(int)trim($_POST['NdkHealthNon']['pph']);
				
				$modelNdkHealthMaster->save();

			//--  Untuk Ndk Health Master --//


			$due_date = explode('|',$_POST['NdkHealthNon']['due_date']);

			for($i=0;$i<count($due_date);$i++){
				$no = (int)$i+1;
				$modelNew = new NdkHealthNon;
				$nota_debet_no = 'NH'.$temp_id.'D2.0'.$no;	

				$modelNew->ndk_health_non_master_id = $modelNdkHealthMaster->id;
				$modelNew->nota_debet_no = $nota_debet_no;
				$modelNew->reg_nonhealth_policy_id = $_POST['NdkHealthNon']['reg_nonhealth_policy_id'];
				$modelNew->sum_insured = $_POST['NdkHealthNon']['sum_insured'];
				$modelNew->rate = $_POST['NdkHealthNon']['rate'];
				$modelNew->total_premium = 0;
				$modelNew->sisa_bayar = 0;
				$modelNew->kurang_bayar = 0;
				$modelNew->jml_bayar = 0;
				$modelNew->is_bayar = '0';
				$modelNew->client_tahu_diskon = $_POST['NdkHealthNon']['client_tahu_diskon'];

				if($i==0){
					//$tpl = $_POST['NdkHealthNon']['tpl'];
					//$adjustment = $_POST['NdkHealthNon']['adjusment']; // Ini Belum Tahun

					$policy_cost = $_POST['NdkHealthNon']['policy_cost'];
					$stampduty_asuransi = $_POST['NdkHealthNon']['stampduty_asuransi'];
					$admin_broker = $_POST['NdkHealthNon']['admin_broker'];
					$discount = $_POST['NdkHealthNon']['discount'];
					$discount_percent = $_POST['NdkHealthNon']['discount_percent'];
					$diskon_asuransi = $_POST['NdkHealthNon']['diskon_asuransi'];
					$diskon_asuransi_percent = $_POST['NdkHealthNon']['diskon_asuransi_percent'];
					$no_endors = $_POST['NdkHealthNon']['endorsement_no'];

				}
				else{
					$policy_cost = 0;
					$admin_broker = 0;
					$discount = 0;
					$discount_percent = 0;
					$stampduty_asuransi = 0;
					$diskon_asuransi = 0;
					$diskon_asuransi_percent = 0;
					$no_endors = 0;
				}

					
					$tpl = $_POST['NdkHealthNon']['tpl']/count($due_date);
					$adjusment = $_POST['NdkHealthNon']['adjusment']/count($due_date);
					$stamp_duty = $_POST['NdkHealthNon']['stamp_duty'];
					
					
					$brokerage = $_POST['NdkHealthNon']['brokerage']/count($due_date);
					$brokerage_percent = $_POST['NdkHealthNon']['brokerage_percent'];
					$ppn = $_POST['NdkHealthNon']['ppn']/count($due_date);
					$ppn_percent = $_POST['NdkHealthNon']['ppn_percent'];
					$pph = $_POST['NdkHealthNon']['pph']/count($due_date);
					$pph_percent = $_POST['NdkHealthNon']['pph_percent'];

				$modelNew->endorsement_no = $no_endors;

				if($client_tahu_diskon==1){
				$modelNew->nominal = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty+(int)$stampduty_asuransi+(int)$admin_broker-(int)$discount;	
				}
				else{
				$modelNew->nominal = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty+(int)$stampduty_asuransi+(int)$admin_broker-(int)$discount-(int)$diskon_asuransi;		
				}
				
				$modelNew->nett_to_insurance = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date))-(int)$diskon_asuransi+(int)$policy_cost+(int)$stampduty_asuransi-(int)$brokerage-(int)$ppn+(int)$pph;

				//$modelNew->nett_to_insurance = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty-(int)$brokerage-(int)$ppn+(int)$pph;


				$modelNew->admin_broker = $admin_broker;
				$modelNew->discount = $discount;
				$modelNew->discount_percent = $discount_percent;
				$modelNew->tpl = $tpl;
				$modelNew->adjusment = $adjusment;
				$modelNew->due_date = date('Y-m-d',strtotime($due_date[$i]));
				$modelNew->brokerage = $brokerage;
				$modelNew->brokerage_percent = $brokerage_percent;
				$modelNew->ppn = $ppn;
				$modelNew->ppn_percent = $ppn_percent;
				$modelNew->pph = $pph;
				$modelNew->pph_percent = $pph_percent;
				$modelNew->note = $note;
				$modelNew->createdby = Yii::app()->user->getId();
				$modelNew->createdtime = date('Y-m-d',strtotime(trim($_POST['NdkHealthNon']['createdtime'])));
				$modelNew->updatedby = Yii::app()->user->getId();
				$modelNew->updatedtime = date('Y-m-d H:i:s');
				$modelNew->installment_ke = $i+1;
				$modelNew->stamp_duty = $stamp_duty;
				$modelNew->stampduty_asuransi = $stampduty_asuransi;
				$modelNew->policy_cost = $policy_cost;
				$modelNew->premium = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date)) ;
				$modelNew->diskon_asuransi = $diskon_asuransi;
				$modelNew->diskon_asuransi_percent = $diskon_asuransi_percent;			
				$modelNew->client_tahu_diskon = (int)$_POST['NdkHealthNon']['client_tahu_diskon'];
				$modelNew->posting = 1;
				$modelNew->type = $_POST['NdkHealthNon']['type'];
				$modelNew->value_brokerage = (int)$_POST['NdkHealthNon']['value_brokerage'];
				$modelNew->save();
			}

				$this->redirect(array('admin'));
			//}
		}

		$this->render('create',array(
			'model'=>$model,
			'model2'=>$model2,
			'model3'=>$model3,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$model2=new MstClient('searchModal2');
		$model3=new RegHealthPolicyNon('search');

		$model2->unsetAttributes();  // clear any default values
		if (isset($_GET['MstClient'])) {
			$model2->attributes=$_GET['MstClient'];
		}
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		
		$temp_id = date("Ymdhis");

		if (isset($_POST['NdkHealthNonMaster'])) {
			
			NdkHealthNonMaster::model()->deleteAll('id='.$id);
			NdkHealthNon::model()->deleteAll('ndk_health_non_master_id='.$id);

			$modelNdkHealthMaster = new NdkHealthNonMaster;

			//--  Untuk Ndk Health Master --//
			$modelNdkHealthMaster->nota_debet_no = '0';
			$modelNdkHealthMaster->reg_nonhealth_policy_id = $_POST['NdkHealthNonMaster']['reg_nonhealth_policy_id'];
			$modelNdkHealthMaster->endorsement_no = $_POST['NdkHealthNonMaster']['endorsement_no'];
			$modelNdkHealthMaster->sum_insured = $_POST['NdkHealthNonMaster']['sum_insured'];
			$modelNdkHealthMaster->rate = $_POST['NdkHealthNonMaster']['rate'];
			$modelNdkHealthMaster->tpl = $_POST['NdkHealthNonMaster']['tpl'];
			$modelNdkHealthMaster->total_premium = 0;
			$modelNdkHealthMaster->sisa_bayar = 0;
			$modelNdkHealthMaster->kurang_bayar = 0;
			$modelNdkHealthMaster->jml_bayar = 0;
			$modelNdkHealthMaster->is_bayar = '0';
			$modelNdkHealthMaster->client_tahu_diskon = $_POST['NdkHealthNonMaster']['client_tahu_diskon'];
				$modelNdkHealthMaster->admin_broker = $_POST['NdkHealthNonMaster']['admin_broker'];
				$modelNdkHealthMaster->discount = (int)$_POST['NdkHealthNonMaster']['discount'];
				$modelNdkHealthMaster->discount_percent = $_POST['NdkHealthNonMaster']['discount_percent'];
				$modelNdkHealthMaster->adjusment = $_POST['NdkHealthNonMaster']['adjusment'];
				$modelNdkHealthMaster->due_date = $_POST['NdkHealthNonMaster']['due_date'];
				$modelNdkHealthMaster->brokerage = (int)$_POST['NdkHealthNonMaster']['brokerage'];
				$modelNdkHealthMaster->brokerage_percent = $_POST['NdkHealthNonMaster']['brokerage_percent'];
				$modelNdkHealthMaster->ppn = (int)$_POST['NdkHealthNonMaster']['ppn'];
				$modelNdkHealthMaster->ppn_percent =$_POST['NdkHealthNonMaster']['ppn_percent'];
				$modelNdkHealthMaster->pph = (int)$_POST['NdkHealthNonMaster']['pph'];
				$modelNdkHealthMaster->pph_percent = $_POST['NdkHealthNonMaster']['pph_percent'];
				$modelNdkHealthMaster->note = $_POST['NdkHealthNonMaster']['note'];
				$modelNdkHealthMaster->createdby = Yii::app()->user->getId();
				$modelNdkHealthMaster->createdtime = date('Y-m-d',strtotime(trim($_POST['NdkHealthNonMaster']['createdtime'])));
				$modelNdkHealthMaster->updatedby = Yii::app()->user->getId();
				$modelNdkHealthMaster->updatedtime = date('Y-m-d H:i:s');
				$modelNdkHealthMaster->installment_ke = 0;
				$modelNdkHealthMaster->stamp_duty = $_POST['NdkHealthNonMaster']['stamp_duty'] * count($due_date);
				$modelNdkHealthMaster->stampduty_asuransi = $_POST['NdkHealthNonMaster']['stampduty_asuransi'];
				$modelNdkHealthMaster->policy_cost = $_POST['NdkHealthNonMaster']['policy_cost'];
				$modelNdkHealthMaster->premium = (int)$_POST['NdkHealthNonMaster']['premium'];
				$modelNdkHealthMaster->diskon_asuransi = (int)$_POST['NdkHealthNonMaster']['diskon_asuransi'];
				$modelNdkHealthMaster->diskon_asuransi_percent = $_POST['NdkHealthNonMaster']['diskon_asuransi_percent'];			
				$modelNdkHealthMaster->client_tahu_diskon = $_POST['NdkHealthNonMaster']['client_tahu_diskon'];
				$modelNdkHealthMaster->posting = 1;
				$modelNdkHealthMaster->type = $_POST['NdkHealthNonMaster']['type'];
				$modelNdkHealthMaster->value_brokerage = (int)$_POST['NdkHealthNonMaster']['value_brokerage'];


				if((int)$_POST['NdkHealthNonMaster']['client_tahu_diskon']==1){
				$modelNdkHealthMaster->nominal = (int)$_POST['NdkHealthNonMaster']['premium']+(int)$_POST['NdkHealthNonMaster']['adjusment']+(int)$_POST['NdkHealthNonMaster']['tpl']+(int)$_POST['NdkHealthNonMaster']['policy_cost']+(int)$_POST['NdkHealthNonMaster']['stamp_duty']+(int)$_POST['NdkHealthNonMaster']['stampduty_asuransi']+(int)$_POST['NdkHealthNonMaster']['admin_broker']-(int)$_POST['NdkHealthNonMaster']['discount'];	
				}
				else{
				$modelNdkHealthMaster->nominal = (int)$_POST['NdkHealthNonMaster']['premium']+(int)$_POST['NdkHealthNonMaster']['adjusment']+(int)$_POST['NdkHealthNonMaster']['tpl']+(int)$_POST['NdkHealthNonMaster']['policy_cost']+(int)$_POST['NdkHealthNonMaster']['stamp_duty']+(int)$_POST['NdkHealthNonMaster']['stampduty_asuransi']+(int)$_POST['NdkHealthNonMaster']['admin_broker']-(int)$_POST['NdkHealthNonMaster']['discount']-(int)$_POST['NdkHealthNonMaster']['diskon_asuransi'];		
				}
				
				$modelNdkHealthMaster->nett_to_insurance = (int)trim($_POST['NdkHealthNonMaster']['premium'])-(int)trim($_POST['NdkHealthNonMaster']['diskon_asuransi'])+(int)trim($_POST['NdkHealthNonMaster']['policy_cost'])+(int)trim($_POST['NdkHealthNonMaster']['stampduty_asuransi'])-(int)trim($_POST['NdkHealthNonMaster']['brokerage'])-(int)trim($_POST['NdkHealthNonMaster']['ppn'])+(int)trim($_POST['NdkHealthNonMaster']['pph']);
				
				$modelNdkHealthMaster->save();

			//--  Untuk Ndk Health Master --//


			$due_date = explode('|',$_POST['NdkHealthNonMaster']['due_date']);

			for($i=0;$i<count($due_date);$i++){
				$no = (int)$i+1;
				$modelNew = new NdkHealthNon;
				$nota_debet_no = 'NH'.$temp_id.'D2.0'.$no;	

				$modelNew->ndk_health_non_master_id = $modelNdkHealthMaster->id;
				$modelNew->nota_debet_no = $nota_debet_no;
				$modelNew->reg_nonhealth_policy_id = $_POST['NdkHealthNonMaster']['reg_nonhealth_policy_id'];
				$modelNew->sum_insured = $_POST['NdkHealthNonMaster']['sum_insured'];
				$modelNew->rate = $_POST['NdkHealthNonMaster']['rate'];
				$modelNew->total_premium = 0;
				$modelNew->sisa_bayar = 0;
				$modelNew->kurang_bayar = 0;
				$modelNew->jml_bayar = 0;
				$modelNew->is_bayar = '0';
				$modelNew->client_tahu_diskon = $_POST['NdkHealthNonMaster']['client_tahu_diskon'];

				if($i==0){
					//$tpl = $_POST['NdkHealthNon']['tpl'];
					//$adjustment = $_POST['NdkHealthNon']['adjusment']; // Ini Belum Tahun

					$policy_cost = $_POST['NdkHealthNonMaster']['policy_cost'];
					$stampduty_asuransi = $_POST['NdkHealthNonMaster']['stampduty_asuransi'];
					$admin_broker = $_POST['NdkHealthNonMaster']['admin_broker'];
					$discount = $_POST['NdkHealthNonMaster']['discount'];
					$discount_percent = $_POST['NdkHealthNonMaster']['discount_percent'];
					$diskon_asuransi = $_POST['NdkHealthNonMaster']['diskon_asuransi'];
					$diskon_asuransi_percent = $_POST['NdkHealthNonMaster']['diskon_asuransi_percent'];

				}
				else{
					$policy_cost = 0;
					$admin_broker = 0;
					$discount = 0;
					$discount_percent = 0;
					$stampduty_asuransi = 0;
					$diskon_asuransi = 0;
					$diskon_asuransi_percent = 0;
				}

					
					$tpl = $_POST['NdkHealthNonMaster']['tpl']/count($due_date);
					$adjusment = $_POST['NdkHealthNonMaster']['adjusment']/count($due_date);
					$stamp_duty = $_POST['NdkHealthNonMaster']['stamp_duty'];
					
					
					$brokerage = (int)$_POST['NdkHealthNonMaster']['brokerage']/count($due_date);
					$brokerage_percent = $_POST['NdkHealthNonMaster']['brokerage_percent'];
					$ppn = (int)$_POST['NdkHealthNonMaster']['ppn']/count($due_date);
					$ppn_percent = $_POST['NdkHealthNonMaster']['ppn_percent'];
					$pph = (int)$_POST['NdkHealthNonMaster']['pph']/count($due_date);
					$pph_percent = $_POST['NdkHealthNonMaster']['pph_percent'];

				$modelNew->endorsement_no = $_POST['NdkHealthNonMaster']['endorsement_no'];

				if($client_tahu_diskon==1){
				$modelNew->nominal = (((int)$_POST['NdkHealthNonMaster']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty+(int)$stampduty_asuransi+(int)$admin_broker-(int)$discount;	
				}
				else{
				$modelNew->nominal = (((int)$_POST['NdkHealthNonMaster']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty+(int)$stampduty_asuransi+(int)$admin_broker-(int)$discount-(int)$diskon_asuransi;		
				}
				
				$modelNew->nett_to_insurance = (((int)$_POST['NdkHealthNonMaster']['premium'])/count($due_date))-(int)$diskon_asuransi+(int)$policy_cost+(int)$stampduty_asuransi-(int)$brokerage-(int)$ppn+(int)$pph;

				//$modelNew->nett_to_insurance = (((int)$_POST['NdkHealthNon']['premium'])/count($due_date))+(int)$tpl+(int)$policy_cost+(int)$stamp_duty-(int)$brokerage-(int)$ppn+(int)$pph;


				$modelNew->admin_broker = $admin_broker;
				$modelNew->discount = $discount;
				$modelNew->discount_percent = $discount_percent;
				$modelNew->tpl = $tpl;
				$modelNew->adjusment = $adjusment;
				$modelNew->due_date = date('Y-m-d',strtotime($due_date[$i]));
				$modelNew->brokerage = $brokerage;
				$modelNew->brokerage_percent = $brokerage_percent;
				$modelNew->ppn = $ppn;
				$modelNew->ppn_percent = $ppn_percent;
				$modelNew->pph = $pph;
				$modelNew->pph_percent = $pph_percent;
				$modelNew->note = $note;
				$modelNew->createdby = Yii::app()->user->getId();
				$modelNew->createdtime = date('Y-m-d',strtotime(trim($_POST['NdkHealthNonMaster']['createdtime'])));
				$modelNew->updatedby = Yii::app()->user->getId();
				$modelNew->updatedtime = date('Y-m-d H:i:s');
				$modelNew->installment_ke = $i+1;
				$modelNew->stamp_duty = $stamp_duty;
				$modelNew->stampduty_asuransi = $stampduty_asuransi;
				$modelNew->policy_cost = $policy_cost;
				$modelNew->premium = ((int)$_POST['NdkHealthNonMaster']['premium'])/count($due_date);
				$modelNew->diskon_asuransi = $diskon_asuransi;
				$modelNew->diskon_asuransi_percent = $diskon_asuransi_percent;			
				$modelNew->client_tahu_diskon = (int)$_POST['NdkHealthNonMaster']['client_tahu_diskon'];
				$modelNew->posting = 1;
				$modelNew->type = $_POST['NdkHealthNonMaster']['type'];
				$modelNew->value_brokerage = (int)$_POST['NdkHealthNonMaster']['value_brokerage'];
				$modelNew->save();
			}

				$this->redirect(array('admin'));
			//}
		}

		$this->render('update',array(
			'model'=>$model,
			'model2'=>$model2,
			'model3'=>$model3,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		//if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			NdkHealthNonMaster::model()->deleteAll('id='.$id);
			NdkHealthNon::model()->deleteAll('ndk_health_non_master_id='.$id);

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if (!isset($_GET['ajax'])) {
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
			}
		//} else {
		//	throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		//}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('NdkHealthNon');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new NdkHealthNonMaster('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['NdkHealthNonMaster'])) {
			$model->attributes=$_GET['NdkHealthNonMaster'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return NdkHealthNon the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=NdkHealthNonMaster::model()->findByPk($id);
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param NdkHealthNon $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='ndk-health-non-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}